import { General } from '../models/General';
import { BaseSkill } from '../skills/BaseSkill';

export class SkillEquipManager {
  private readonly MAX_EQUIPPED_SKILLS = 2;

  public equipSkill(general: General, skill: BaseSkill): boolean {
    if (!this.canEquipSkill(general, skill)) {
      return false;
    }

    general.equippedSkills.push(skill);
    return true;
  }

  public unequipSkill(general: General, skillId: string): boolean {
    const index = general.equippedSkills.findIndex(s => s.id === skillId);
    if (index === -1) {
      return false;
    }

    general.equippedSkills.splice(index, 1);
    return true;
  }

  private canEquipSkill(general: General, skill: BaseSkill): boolean {
    // 检查装备数量限制
    if (general.equippedSkills.length >= this.MAX_EQUIPPED_SKILLS) {
      console.error('Maximum equipped skills reached');
      return false;
    }

    // 检查是否已装备相同技能
    if (general.equippedSkills.some(s => s.id === skill.id)) {
      console.error('Skill already equipped');
      return false;
    }

    // 检查是否与固有技能重复
    if (general.skills.some(s => s.id === skill.id)) {
      console.error('Skill conflicts with innate skills');
      return false;
    }

    return true;
  }
}
